<template>

  <div class="app-container">

    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="用户姓名">
        <el-input v-model="formInline.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="用户类型">
        <el-select v-model="formInline.type" placeholder="用户类型">
          <el-option label="--请选择--" value=""></el-option>
          <el-option label="管理员" value="0"></el-option>
          <el-option label="普通用户" value="1"></el-option>
          <el-option label="车库管理" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button type="success" @click="onAdd">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" >
        <template slot-scope="scope">
          {{ scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column label="账号">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column label="姓名"  align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户类型" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.type == 0 ? '管理员' :scope.row.type == 2? '车库管理员' : '普通用户' }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" >
        <template v-slot="scope">
          <el-button type="primary" size="mini" @click="toEdit(scope.row)">编辑</el-button>
          <el-button type="danger"  size="mini" @click="toDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <el-pagination
      :page-size="pageSize"
      :page-count="pages"
      :current-page="pageNum"
      @current-change="changePageNum"
      background
      layout="prev, pager, next"
      :total="total">
    </el-pagination>

    <el-dialog
      destroy-on-close
      title="添加/编辑管理员"
      :visible.sync="dialogVisible"
      width="60%">

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="用户类型" prop="type">
          <el-select v-model="ruleForm.type" placeholder="请选择用户类型">
            <el-option label="车库管理员" :value="2"></el-option>
            <el-option label="普通用户" :value="1"></el-option>
            <el-option label="超级管理员" :value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>
  </div>

</template>
<script >

import {getPage, saveOrUpdate, del, validate,} from "@/api/user";
import {Message} from "element-ui";

export default {
  name: "user",
  data(){
    var validateUserName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号'));
      } else {
        //发送请求
        console.log(this.ruleForm.id)
        validate({username:value,id:this.ruleForm.id}).then( res => {
          callback();
        })

      }
    };
    return {
      dialogVisible: false,
      ruleForm:{
        id: '',
        name: '',
        username: '',
        password: '',
        type: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入用户姓名', trigger: 'blur'},
        ],
        username: [
          {required: true, message: '请输入用户账号', trigger: 'blur'},
          { validator: validateUserName, trigger: 'blur' }
        ],
        password: [
          {required: true, message: '请输入用户密码', trigger: 'blur'},
        ],
        type: [
          {required: true, message: '请选择用户类型', trigger: 'blur'}
        ],
      },
      formInline:{
        name:'',
        type:''
      },
      list: [],
      pageNum:1,
      pageSize: 3,
      total: 0,
      pages: 0,
      name:'',
      listLoading: true
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    onAdd(){
      this.ruleForm={}
      this.dialogVisible=true
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveOrUpdate(this.ruleForm).then( res=>{
            console.log(res)
            this.$message.success("操作成功")
            this.dialogVisible=false
            this.fetchData()
          })
        } else {
          this.$message.error("请按提示填写表单")
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    toEdit(row){
      this.$set(this.ruleForm,'id',row.id)
      this.$set(this.ruleForm,'username',row.username)
      this.$set(this.ruleForm,'name',row.name)
      this.$set(this.ruleForm,'password',row.password)
      this.$set(this.ruleForm,'type',row.type)
      // this.ruleForm.id=row.id
      // this.ruleForm.username=row.username
      // this.ruleForm.name=row.name
      // this.ruleForm.password=row.password
      // this.ruleForm.type=row.type
      this.dialogVisible=true

    },
    toDel(id){

      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        del(id).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.fetchData()
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    onSearch(){
      this.pageNum=1
      this.fetchData()
    },

    changePageNum(p){
      this.pageNum=p
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getPage(this.formInline,this.pageNum,this.pageSize).then(res => {
        this.list = res.data.records
        this.pages = res.data.pages
        this.pageNum = res.data.current
        this.total=res.data.total
        this.listLoading = false
      })
    }
  },


}

</script>



<style scoped>

</style>
